import 'package:furaha_mkopo/commonTool/fm_report_event_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_time_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_detail_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_firm_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_ought_api.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_ought_entity.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

import '../api/fm_home_ourselves_api.dart';

class FMHomeDetailController extends GetxController {
  FMHomeDetailEntity? detailEntity;
  Position? position;
  int startTime = 0;

  @override
  void onInit() {
    super.onInit();
    startTime = FMTimeTool.currentTime;
  }

  void onRequestDetailData(String? listen,String? title) {
    if(listen != null && listen.isNotEmpty){
      onRequestNoEmptyListen(listen);
    }else {
      onRequestEmptyListen(title);
    }
  }

  void onRequestNoEmptyListen(String listen){
    FMProgressHud.showLoading();
    FMHomeDetailApi api = FMHomeDetailApi(listen);
    api
        .send<FMHomeDetailEntity, FMHomeDetailEntity>(
        decodeType: FMHomeDetailEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        update();
        return;
      }
      for(int i = 0;i < (value.data!.cost?.length??0);i++){
        Cost cost = value.data!.cost![i];
        cost.imageBg = "assets/home/ic_detail_step${i + 1}_bg.png";
        cost.imageTitleBg = "assets/home/ic_detail_step_title_bg.png";
        cost.imageCardBg = "assets/home/ic_detail_step${i + 1}_card_bg.png";
        cost.imageBtnBg = cost.genuine != 1 ? "assets/home/ic_detail_step_btn.png" : "assets/home/ic_detail_step_completed_btn.png";
        cost.itemHeight = i == 0 ? 181 : 206;
      }
      detailEntity = value.data!;
      update();
    });
  }

  void onRequestEmptyListen(String? title){
    FMProgressHud.showLoading();
    FMHomeFirmApi firmApi = FMHomeFirmApi();
    firmApi.send<FMHomeDetailEntity,FMHomeDetailEntity>(decodeType: FMHomeDetailEntity()).then((value){
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        update();
        return;
      }
      for(int i = 0;i < (value.data!.cost?.length??0);i++){
        Cost cost = value.data!.cost![i];
        cost.imageBg = "assets/home/ic_detail_step${i + 1}_bg.png";
        cost.imageTitleBg = "assets/home/ic_detail_step_title_bg.png";
        cost.imageCardBg = "assets/home/ic_detail_step${i + 1}_card_bg.png";
        cost.imageBtnBg = cost.genuine != 1 ? "assets/home/ic_detail_step_btn.png" : "assets/home/ic_detail_step_completed_btn.png";
        cost.itemHeight = i == 0 ? 181 : 206;
      }
      detailEntity = value.data;
      update();
    });
  }

  void onRequestBankInfo(String listen){
    FMHomeOurselvesApi ourselvesApi = FMHomeOurselvesApi(listen);
    FMProgressHud.showLoading();
    ourselvesApi.send().then((value){
      FMProgressHud.hideLoading();
    });
  }
}
